/*
   Copyright (c) [2023] [魔凤啸天]
   [MFUI] is licensed under Mulan PSL v2.
   You can use this software according to the terms and conditions of the Mulan PSL v2. 
   You may obtain a copy of Mulan PSL v2 at:
            http://license.coscl.org.cn/MulanPSL2 
   THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.  
   See the Mulan PSL v2 for more details.  
*/
#include "textbutton.hpp"
#include "layout.hpp"
#include "listlayout.hpp"
#include "texturerect.hpp"
#include "texturebutton.hpp"
#include "radiobutton.hpp"
#include "textedit.hpp"
#include "label.hpp"
#include "canvas.hpp"
#include "progressbar.hpp"
#include<unistd.h>

using namespace MFUI;

class MyButton:public TextButton{
	unsigned int count;
	TextureRect* b;
	~MyButton()override{b->deref();}
	void _update(){
		auto s="当前计数："+std::to_string(count);
		settext(s);
	}
	public:
	MyButton(Font* font,TextureRect* b):b(b),TextButton(emptytext,font),count(0){
		_update(),b->ref();
	};
	void released(Window* w,SDL_FPoint& p)override{
		count++;
		_update();
		b->setalpha(b->getalpha()>>1);
	}
};

class SwapButton:public TextButton{
	ListLayout* layout;

	~SwapButton()override{layout->deref();}
	public:
	SwapButton(Font* font,ListLayout* l):TextButton("交换13",font),layout(l){
		l->ref();
	}
	void released(Window* w,SDL_FPoint& p)override{
		layout->swapchild(0,2);
	}
};

class Radio1:public RadioButton{
	TextureRect* t;
	~Radio1(){t->deref();}
	public:
	Radio1(TextureRect* t):t(t),RadioButton({0,128,128,255},false){t->ref();}
	void changed()override{
		if(getvalue())t->setalpha(0);
	}
};

class Radio2:public RadioButton{
	TextureRect* t;
	~Radio2(){t->deref();}
	public:
	Radio2(TextureRect* t):t(t),RadioButton({0,128,128,255},false){t->ref();}
	void changed()override{
		if(getvalue())t->setalpha(127);
	}
};

class Radio3:public RadioButton{
	TextureRect* t;
	~Radio3(){t->deref();}
	public:
	Radio3(TextureRect* t):t(t),RadioButton({0,128,128,255},false){t->ref();}
	void changed()override{
		if(getvalue())t->setalpha(255);
	}
};

class AlphaProgress:public ProgressBar{
	TextureRect* t;
	public:
	AlphaProgress(TextureRect* r):t(r),ProgressBar({0,128,128,255},{10,200,200,255},1.F){}
	void changed()override{
		t->setalpha(255*value);
	}
	~AlphaProgress()override{}
};

inline Node* descitem(const char* text,Font* font,Node* down){
	return new VBoxLayout({255,255,255,120},0.F,0.F,0.F,0.F,0.01F,
	{new Label(text,font),down});
}

int main(int argc,char* argv[]){
	if(openlib()){
		auto path="./font.ttf";
		TTF_Font* f=TTF_OpenFont(path,18);
		if(!f){
			if(SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,"字体初始化失败",path,nullptr))
			fprintf(stderr,"font init failed:%s\n",path);
			return -1;
		}
		Font font(f);
		int windowwidth=1280,windowheight=720;
		auto t1=new TextureRect(nullptr),t2=new TextureRect(nullptr);
		auto t3=new TextureButton(nullptr);
		auto r1=new Radio1(t1);
		auto r2=new Radio2(t1);
		auto r3=new Radio3(t1);
		auto l=new VListLayout(3,{255,255,255,120},{});
		auto w=new Window("test",windowwidth,windowheight,SDL_WINDOW_RESIZABLE|SDL_WINDOW_OPENGL,new Node());
		l->addchildren(w,{
			new MyButton(&font,t1),
			t2,
			new SwapButton(&font,l),
			t3,
			new AlphaProgress(t1)
		});
		r1->connect({r2,r3});
		auto root=new VSepLayout({0,128,128,255},0.005F,0.005F,0.005F,0.005F,0.1F,{
			new Label("你好，MFUI",&font,align_m,50),
			new VBoxLayout({0,0,0,120},0.005F,0.005F,0.005F,0.005F,0.005F,{
			new HBoxLayout({0,0,0,120},0.F,0.F,0.F,0.F,0.01F,
			{t1,new HBoxLayout({0,0,0,120},0.F,0.F,0.F,0.F,0.01F,
			{descitem("透明",&font,r1),descitem("半透明",&font,r2),descitem("原图",&font,r3)})}),
			new HBoxLayout({255,255,255,120},0.005F,0.005F,0.005F,0.005F,0.005F,{
				l,
				new HListLayout(1,{200,200,200,120},{
					new TextEdit(emptytext,"请输入内容",&font),new Canvas(w,400,200)
				})
			})
			})
		});
		root->setsize({(float)windowwidth,(float)windowheight});
		auto t=w->texturefromfile("./test.jpg",SDL_BLENDMODE_BLEND);
		t1->settexture(t),t2->settexture(t),t3->settexture(t);
		w->connectwindowresize(root);
		w->setroot(root);
		Window::main(30);
		SDL_DestroyTexture(t);
		TTF_CloseFont(f);
		closelib();
		return 0;
	}
	return -1;
}